﻿@using CubicFWNet.Data.Models
@using CubicFWNet.Data.ViewModal
@model CubicFWNet.Data.Models.Order
@{
    //var product = ViewBag.Product as Product;
    var customer = ViewBag.Customer as Customer;
    var proList = ViewBag.ProList as List<ProductTechnologyView>;
    var operationName = ViewBag.OperationName;
    var operationTime = ViewBag.OperationTime;
    var generateNum = ViewBag.GenerateNum;
    var invoceTypelist = ViewBag.InvoceTypeList as IEnumerable<InvoceType>;
}
<section class="content-header">
    <h1>
        添加发货单
    </h1>
    <ol class="breadcrumb">
        <li><a href="javascript:void(0);"><i class="fa fa-dashboard"></i>@Resources.Global.PageTitle</a></li>
        <li><a href="@Url.Action("Index", "Home")">首页</a></li>
        <li class="active"><a href="@Url.Action("InvoiceQuery", "Order")">发货单查询</a></li>
    </ol>
</section>
<section class="content">
    <div class="box box-solid">
        <!-- /.box-header -->
        <div class="box-body">
            <div class="row">
                @if (Model != null && customer != null)
                {
                    <form class="form-horizontal" id="frmedit" action="@Url.Action("AddInvoice", "Order")" method="post">
                        <input type="hidden" id="OrderId" name="OrderId" value="@Model.Id" />
                        <input type="hidden" id="StoreName" name="StoreName" value="" />
                        <div class="box-body">
                            <div class="form-group col-md-6">
                                <label class="col-sm-3 control-label">发货单号</label>
                                <div class="col-sm-9">
                                    <input type="text" class="form-control" id="Number" name="Number" value="@generateNum" readonly="readonly">
                                </div>
                            </div>
                            <div class="form-group col-md-6">
                                <label class="col-sm-3 control-label">订单号</label>
                                <div class="col-sm-9">
                                    <input type="text" class="form-control" id="OrderNumber" name="OrderNumber" value="@Model.Number" readonly="readonly">
                                </div>
                            </div>
                            <div class="form-group col-md-6">
                                <label class="col-sm-3 control-label">收货单位</label>
                                <div class="col-sm-9">
                                    <input type="text" class="form-control" id="TakeCompany" name="TakeCompany" value="@customer.Name" readonly="readonly">
                                </div>
                            </div>
                            <div class="form-group col-md-6">
                                <label class="col-sm-3 control-label">简称</label>
                                <div class="col-sm-9">
                                    <input type="text" class="form-control" id="customerShortName" name="customerShortName" value="@customer.ShortName" readonly="readonly">
                                </div>
                            </div>
                            <div class="form-group col-md-6">
                                <label class="col-sm-3 control-label">填单日期</label>
                                <div class="col-sm-9">
                                    <input type="text" class="form-control Timedatepicker" id="operationTime" name="operationTime" placeholder="" value="@DateTime.Now.ToString("yyyy-MM-dd")" required>
                                </div>
                            </div>
                            <div class="form-group col-md-6">
                                <label class="col-sm-3 control-label">发货日期</label>
                                <div class="col-sm-9">
                                    <input type="text" class="form-control Timedatepicker" id="DliveryTime" name="DliveryTime" value="" required>
                                </div>
                            </div>
                            <div class="form-group col-md-6">
                                <label class="col-sm-3 control-label">预计到货日期</label>
                                <div class="col-sm-9">
                                    <input type="text" class="form-control Timedatepicker" id="ReceivedTime" name="ReceivedTime" value="@Model.SendTime" required>
                                </div>
                            </div>
                            <div class="form-group col-md-6" style="display: none">
                                <label class="col-sm-3 control-label">收货人</label>
                                <div class="col-sm-9">
                                    <input type="text" class="form-control" id="ReceivePeople" name="ReceivePeople" value="">
                                </div>
                            </div>
                            <div class="form-group col-md-6">
                                <label class="col-sm-3 control-label">发货人</label>
                                <div class="col-sm-9">
                                    <input type="text" class="form-control" id="SendPeople" name="SendPeople" value="" required>
                                </div>
                            </div>

                            <div class="form-group col-md-12" style="height: 1px;"></div>
                            <div class="form-group col-md-6">
                                <label class="col-sm-3 control-label">送货要求</label>
                                <div class="col-sm-9">
                                    <textarea class="form-control" rows="3" placeholder="请输入 ..." name="DliveryDemand" id="DliveryDemand"></textarea>
                                </div>
                            </div>
                            <div class="form-group col-md-12" style="height: 1px;"></div>
                            <div id="json-content" class="col-md-12 ">
                                @*col-md-offset-1*@
                                @*<span class="btn btn-primary submitbtn" id="print" onclick="print()">打印</span>*@

                                <div class="form-group col-md-12" style="height: 1px;"></div>

                                <table class="footable table table-stripped table-bordered table-hover" id="footable">
                                    <thead>
                                        <tr>
                                            <th data-sort-ignore="true">
                                                品名
                                            </th>
                                            <th data-hide="phone" data-sort-ignore="true">
                                                尺寸宽-mm
                                            </th>
                                            <th data-hide="phone" data-sort-ignore="true">
                                                尺寸长-mm
                                            </th>
                                            <th data-hide="phone" data-sort-ignore="true">
                                                基本单位
                                            </th>
                                            <th data-hide="phone" data-sort-ignore="true" class="col-md-2">
                                                数量
                                            </th>
                                            <th data-hide="phone" data-sort-ignore="true" class="col-md-2">
                                                价格
                                            </th>
                                            <th data-hide="phone" data-sort-ignore="true" class="col-md-1">
                                                增值税
                                            </th>
                                            <th data-hide="phone" data-sort-ignore="true" class="col-md-2">
                                                金额
                                            </th>
                                            <th data-hide="phone" data-sort-ignore="true">
                                                订货数量
                                            </th>
                                            <th data-hide="phone" data-sort-ignore="true">
                                                库存剩余
                                            </th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        @if (proList != null)
                                        {
                                            var totalWorth = Convert.ToDecimal(0);
                                            foreach (var item in proList)
                                            {
                                                //var totalMoney = item.Price * item.Amount * Convert.ToDecimal(1.17);
                                                var amount = "0";
                                                if (item.StorageAmount >= item.Amount)
                                                {
                                                    amount = item.Amount.ToString("f2");
                                                }
                                                else
                                                {
                                                    amount = item.StorageAmount.ToString("f2");
                                                }
                                                var totalMoney = Convert.ToDecimal(amount) * item.Price * Convert.ToDecimal(1.17);
                                                totalWorth += Convert.ToDecimal(totalMoney);
                                                <tr class="tr">
                                                    <td>
                                                        @if (string.IsNullOrEmpty(item.ProductNameOth))
                                                        {
                                                            @item.ProductName
                                                            <input type="hidden" class="form-control" data-des="品名" id="ProductName" name="ProductName" value="@item.ProductName">
                                                        }
                                                        else
                                                        {
                                                            @item.ProductNameOth
                                                            <input type="hidden" class="form-control" data-des="品名" id="ProductName" name="ProductName" value="@item.ProductNameOth">
                                                        }
                                                        <input type="hidden" class="form-control" data-des="产品Id" id="ProductId" name="ProductId" value="@item.ProductId">
                                                    </td>
                                                    <td>
                                                        @Html.DisplayFor(modelItem => item.SizeWidth)
                                                        <input type="hidden" class="form-control" data-des="尺寸宽-mm" id="ProductSizeWidth" name="ProductSizeWidth" value="@item.SizeWidth">
                                                    </td>
                                                    <td>
                                                        @Html.DisplayFor(modelItem => item.SizeLenth)
                                                        <input type="hidden" class="form-control" data-des="尺寸长-mm" id="ProductSizeLenth" name="ProductSizeLenth" value="@item.SizeLenth">
                                                    </td>
                                                    <td>
                                                        @Html.DisplayFor(modelItem => item.Unit)
                                                        <input type="hidden" class="form-control" data-des="基本单位" id="ProductUnit" name="ProductUnit" value="@item.Unit">
                                                    </td>
                                                    <td>
                                                        <input type="text" class="form-control okeydown itemAmount" data-des="数量" data-itemprice="@item.Price" data-amount="@item.Amount" data-storageamount="@item.StorageAmount" data-proname="@item.ProductNameOth" id="ProductAmount" name="ProductAmount" value="@amount" required>
                                                        @*@Html.DisplayFor(modelItem => item.Amount)*@
                                                    </td>
                                                    <td>
                                                        <input type="text" class="form-control okeydown itemPrice" data-des="价格" id="ProductPrice" name="ProductPrice" value="@item.Price" readonly="readonly">
                                                        @*@Html.DisplayFor(modelItem => item.Price)*@
                                                    </td>
                                                    <td>
                                                        <input type="text" class="form-control okeydown productTaxClass" data-des="增值税" data-itemprice="@item.Price" id="ProductTax" name="ProductTax" value="1.17" required>
                                                    </td>
                                                    <td>
                                                        <input type="text" class="form-control okeydown itemMoney" data-des="金额" id="TotalMoney" name="TotalMoney" value="@totalMoney.ToString("f2")" readonly="readonly">
                                                    </td>
                                                    <td>
                                                        <span style="color: dodgerblue;">@item.Amount</span>
                                                    </td>
                                                    <td>
                                                        <span style="color: dodgerblue;">@item.StorageAmount</span>
                                                    </td>
                                                </tr>
                                            }
                                            <input type="hidden" id="totalWorth" value="@totalWorth.ToString("f2")" />
                                        }
                                    </tbody>
                                </table>
                            </div>

                            <div class="form-group col-md-12" style="height: 1px;"></div>
                            <div class="form-group col-md-6">
                                <label class="col-sm-3 control-label">发货方式</label>
                                <div class="col-sm-9">
                                    <select class="form-control" id="Type" name="Type">
                                        <option value="0" selected="selected">==请选择==</option>
                                        @if (invoceTypelist != null)
                                        {
                                            foreach (var item in invoceTypelist)
                                            {
                                                <option value="@item.Id">@item.TypeName</option>
                                            }
                                        }
                                    </select>
                                </div>
                            </div>
                            <div class="form-group col-md-12" style="height: 1px;"></div>
                            <div id="car" class="hidden">
                                <div class="form-group col-md-6">
                                    <label class="col-sm-3 control-label">货物总值</label>
                                    <div class="col-sm-9">
                                        <input type="text" class="form-control okeydown" id="Worth" name="Worth" value="" />
                                    </div>
                                </div>
                                <div class="form-group col-md-6">
                                    <label class="col-sm-3 control-label">送货要求</label>
                                    <div class="col-sm-9">
                                        <textarea class="form-control" rows="3" placeholder="请输入 ..." name="Demand" id="Demand"></textarea>
                                    </div>
                                </div>

                            </div>
                            <div id="express" class="hidden">
                                <div class="form-group col-md-6">
                                    <label for="operationName" class="col-sm-3 control-label">快递名称</label>
                                    <div class="col-sm-9">
                                        <input type="text" class="form-control" id="ExpTypeName" name="ExpTypeName" value="" />
                                    </div>
                                </div>
                                <div class="form-group col-md-6">
                                    <label for="operationName" class="col-sm-3 control-label">快递编号</label>
                                    <div class="col-sm-9">
                                        <input type="text" class="form-control" id="ExpNumber" name="ExpNumber" value="" />
                                    </div>
                                </div>
                                <div class="form-group col-md-6" style="display: none">
                                    <label for="operationName" class="col-sm-3 control-label">付费方式</label>
                                    <div class="col-sm-9">
                                        <select class="form-control" id="PayType" name="PayType">
                                            <option value="1">模拟1</option>
                                            <option value="2">模拟2</option>
                                        </select>
                                    </div>
                                </div>
                            </div>
                            <div class="form-group col-md-12" style="height: 1px;"></div>
                            <div class="form-group col-md-6">
                                <label for="operationName" class="col-sm-3 control-label">备注</label>
                                <div class="col-sm-9">
                                    <textarea class="form-control" rows="3" placeholder="请输入 ..." name="Memo" id="Memo"></textarea>
                                </div>
                            </div>
                            <div class="form-group col-md-12" style="height: 1px;"></div>
                            <div class="form-group col-md-6">
                                <label for="operationName" class="col-sm-3 control-label">操作人</label>
                                <div class="col-sm-9">
                                    <input type="text" class="form-control" id="operationName" name="operationName" value="@operationName" readonly="readonly">
                                </div>
                            </div>
                            <div class="form-group col-md-6">
                                <label for="operationTime" class="col-sm-3 control-label">日期</label>
                                <div class="col-sm-9">
                                    <input type="text" class="form-control" id="operationTime" name="operationTime" value="@operationTime" readonly="readonly">
                                </div>
                            </div>
                        </div>
                        <div class="box-footer" style="width: 80%; margin-left: 20px;">
                            <a role="button" href="javascript:void(0)" class="btn btn-primary submitbtn" onclick="formsubmit(1)">确定</a>
                            <a class="btn btn-primary submitbtn" href="@Url.Action("InvoiceQuery", "Order")">返回</a>
                        </div>
                    </form>
                }
            </div>
        </div>
    </div>
</section>

<div id="loadingPage" style="display: none;">
    <div class="loading-shade"></div>
    <div class="loading-content" onclick="$.loading(false)">数据加载中，请稍后…</div>
</div>

@section Scripts{
    <script src="~/Scripts/jquery.jqprint-0.3.js"></script>

    <script type="text/javascript">
        $(document).ready(function () {
            $('#footable').footable();
            //货物总值
            var totalWorth = $("#totalWorth").val();
            $("#Worth").val(totalWorth);

            //日期控件
            $(".Timedatepicker").datepicker({
                autoclose: true,
                language: 'zh', //汉化
                format: "yyyy-mm-dd", //选择日期后，文本框显示的日期格式
                minView: "month" //选择日期后，不会再跳转去选择时分秒
            });

            $("#frmedit").validate({
                submitHandler: function (form) {
                    var postData = $(form).formSerialize();
                    postData.Data = getParam(); //JSON.stringify(postData._Data);
                    $.submitForm({
                        url: $(form).attr("action"),
                        param: postData,
                        success: function () {
                            //$.reload();
                            $.modalAlert("操作成功", "success");
                            window.location.href = '@Url.Action("Orders", "CustomerService")';
                        }
                    });
                }
            });


            $(document).on("change", "#StoreType", function () {
                var storeTypeName = this.options[this.selectedIndex].innerHTML;
                $("#StoreName").val(storeTypeName);
            });
            $("#StoreType").change(); //第一个默认点击状态

        });

        var type = 1;
        var verifyAmount = 0;
        var verifyAmountFlag = false;
        var totaltax = 1.17;
        function formsubmit(t) {
            type = t;
            verifyParam();

            var value = $("#Type").val();
            if (!verifyAmountFlag) {
                $.modalAlert("发货数量不可全部为0", "warning");
                return;
            }
            if (value == "" || value == "0") {
                $.modalAlert("请选发货方式", "warning");
                return;
            }

            top.layer.confirm('本次增值税为' + totaltax, {
                icon: "fa-exclamation-circle",
                title: "系统提示",
                btn: ['确认', '取消'],
                btnclass: ['btn btn-primary', 'btn btn-danger']
            }, function () {
                $("#frmedit").submit();
            }, function () {

            });
            
        }

        $("#Type").on("change", function () {
            var value = $(this).val();
            //快递
            if (value == "0") {
                $("#car").addClass("hidden");
                $("#express").addClass("hidden");
            } else {
                if (value == "2") {
                    $("#express").removeClass("hidden");
                    $("#car").addClass("hidden");
                } else {
                    $("#express").addClass("hidden");
                    $("#car").removeClass("hidden");
                }
                if (value == "1") {
                    $("#car").removeClass("hidden");
                    $("#express").addClass("hidden");
                } else {
                    $("#car").addClass("hidden");
                    $("#express").removeClass("hidden");
                }
            }
        });

        $(".itemAmount").keyup(function () {
            var amount = $(this).val();
            if (isNaN(amount)) {
                amount = 0;
                $(this).val(0);
            }

            var storageamount = $(this).attr('data-storageamount') / 1;
            var proName = $(this).attr('data-proName');
            if (amount > storageamount) {
                $(this).val(storageamount);
                amount = storageamount;
                $.modalAlert(proName + "库存不足", "warning");
            }

            var total = $(this).attr('data-amount') / 1;
            if (total < amount) {
                $(this).val(total);
                amount = total;
            }
            amount = amount / 1;
            var price = $(this).attr('data-itemprice');
            var tax = $(this).parents(".tr").find(".productTaxClass").val();
            if (isNaN(tax)) {
                tax = 1.17;
            }
            tax = tax / 1;
            var b = (amount * price * tax).toFixed(2);
            $(this).parents(".tr").find(".itemMoney").val(b);
            worthCa();
        });

        $(".productTaxClass").keyup(function () {
            var tax = $(this).val() / 1;
            var price = $(this).attr('data-itemprice');
            var amount = $(this).parents(".tr").find(".itemAmount").val();
            if (isNaN(tax)) {
                tax = 1.17;
                $(this).val(1.17);
            }
            tax = tax / 1;
            var b = (amount * price * tax).toFixed(2);
            $(this).parents(".tr").find(".itemMoney").val(b);
        });


        $(".okeydown").on("keydown", function () {
            var k = event.keyCode;
            if ((k <= 57 && k >= 48) || (k <= 105 && k >= 96) || (k == 8) || (k == 110) || (k == 190)) {
                return true;
            } else {
                return false;
            }
        });

        function print() {
            $("#footable").jqprint();
        }
        //计算货物总值
        function worthCa() {
            var totalWorth = 0;
            $("#json-content").find('.tr').each(function (r, i) { //input,select,textarea
                $(i).find("input[name='TotalMoney']").each(function (m) {
                    var $this = $(this);
                    //var id = $this.attr('id');
                    totalWorth += ($this.val()) / 1;
                });
            });
            $("#Worth").val(totalWorth);
        }
        //function getParam() {
        //    var postdata = [];
        //    //主键
        //    $("#json-content").find('.tr').each(function(r, i) { //input,select,textarea
        //        var postdataItem = [];
        //        $(i).find('input').each(function(m) {
        //            var $this = $(this);
        //            var id = $this.attr('id');
        //            var obj = {};
        //            obj = { des: $this.attr("data-des"), name: id, value: $this.val() };
        //            postdataItem.push(obj);
        //        });
        //        postdata.push(postdataItem);
        //    });
        //    return JSON.stringify(postdata);
        //}

        function getParam() {
            var postdata = [];
            //主键
            $("#json-content").find('.tr').each(function (r, i) { //input,select,textarea
                var postdataItem = {};
                $(i).find('input').each(function (m) {
                    var $this = $(this);
                    var id = $this.attr('id');
                    postdataItem[id] = $this.val();
                });
                postdata.push(postdataItem);
            });
            return JSON.stringify(postdata);
        }


        function verifyParam() {
            //主键
            $("#json-content").find('.tr').each(function (r, i) { //input,select,textarea
                $(i).find('input').each(function (m) {
                    var $this = $(this);
                    var id = $this.attr('id');
                    var val = $this.val();
                    if (id == "ProductAmount") {
                        if (isNaN(val) || val == "") {
                            val = 0;
                        }
                        verifyAmount = val / 1;
                        if (verifyAmount > 0) {
                            verifyAmountFlag = true;
                        }
                    }
                    if (id == "ProductTax") {
                        totaltax = val;
                    }
                });
            });
        }

        //查库存
        function ShowInventory(orderID, productID) {
            $.ajax({
                url: "/Order/ShowInventory",
                type: "Post",
                data: { oid: orderID, pid: productID },
                dataType: "json",
                async: false,
                success: function (data) {
                    $.modalAlert("库存数量：" + data, "success");
                }
            });
        }



        /* $("#printContainer").jqprint({
             debug: false, //如果是true则可以显示iframe查看效果（iframe默认高和宽都很小，可以再源码中调大），默认是false
             importCSS: true, //true表示引进原来的页面的css，默认是true。（如果是true，先会找$("link[media=print]")，若没有会去找$("link")中的css文件）
             printContainer: true, //表示如果原来选择的对象必须被纳入打印（注意：设置为false可能会打破你的CSS规则）。
             operaSupport: true//表示如果插件也必须支持歌opera浏览器，在这种情况下，它提供了建立一个临时的打印选项卡。默认是true
         });*/
    </script>
}